@ECHO off

MODE 80,30

CLS
echo.
echo ----- SMG1 Mod ISO Builder (v0.4)
echo ----- By Humming Owl
echo.

:: ===============================
:: Get to the path the BAT file is
:: ===============================

cd %~dp0

:: ================
:: mods data storage
:: ================

call .\tools\mods_data.bat

:: =============================
:: actions before starting patch
:: =============================

if exist ".\savefiles" (rmdir /s /q ".\savefiles")

if exist ".\temp" (rmdir /s /q ".\temp")

if not exist ".\mods" (mkdir ".\mods")

for /d %%a in (".\mods\*") do rd "%%a" /q /s

if exist ".\*.tmp" (del /S *.tmp >nul)

:: ===================================================
:: Key command to work with variables inside variables
:: ===================================================

SETLOCAL EnableDelayedExpansion

:: =====
:: Tools
:: =====

set WGET=tools\wget.exe
set EXTRACT=tools\7z.exe
set PATCH=tools\patch.exe
set WIT=tools\wit.exe


:: ==========
:: check_file
:: ==========

if not "%~x1" == ".iso" ( 
   if not "%~x1" == ".wbfs" (

      echo.
      echo File dropped IS NOT ISO/WBFS. 
      echo Verify file and try again.
      echo.
      echo Press any key to exit.
      echo.

      pause >nul

      exit
   )
)

%WIT% verify --test --quiet "%~f1"
if %ERRORLEVEL% NEQ 0 (
   echo.
   echo File dropped is ISO/WBFS, but not a valid one.
   echo Verify file and try again.
   echo.
   echo Press any key to exit.
   echo.

   pause >nul

   exit
)

echo.
echo File dropped IS a valid ISO/WBFS.

FOR /F "tokens=*" %%g IN ('%WIT% ID6 "%~f1"') do (set og_gameid=%%g)

if not "%og_gameid%" == "RMGE01" (
   if not "%og_gameid%" == "RMGP01" (
      if not "%og_gameid%" == "RMGJ01" (
         if not "%og_gameid%" == "RMGK01" (
            echo But is not an original SMG1 file.
            echo Verify the file and try again.
            echo.
            echo Press any key to exit.
            echo.

            pause >nul

            exit
         )
      )
   )
)


:: ============
:: region check
:: ============

if "%og_gameid%" == "RMGE01" (set reg=e)
if "%og_gameid%" == "RMGP01" (set reg=p)
if "%og_gameid%" == "RMGJ01" (set reg=j)
if "%og_gameid%" == "RMGK01" (set reg=k)

:: ============

echo And its an original SMG1 file.
echo Ready to go.
echo.
echo Press any key if you are ready.
echo.

pause

CLS
:: ==================
:: SMG1 Rom Hack List
:: ==================

echo.

echo --- SMG1 Hack list:
echo.
echo 1. Kaizo Mario Galaxy (v1.2)
echo 2. Super Mario Galaxy: Transformationless Challenge (v1.0.1)
echo 3. Super Mario Galaxy: The Green Stars (v1.1) (has some issues)
echo 4. Super Mario Galaxy: The Kaizo Green Stars (v1.6.1)
echo 5. Super Mario Galaxy: Underwater Edition
echo 6. Super Mario Galaxy: No Gravity Edition (v1.0)
echo 7. Super Mario Galaxy: Cosmic Caos (v1.3.5)
echo 8. Super Mario Galaxy: Daredevil Challenge
echo 9. Super Mario Galaxy: The Green Star Festival (v1.1 - E/P/J/K/W)
echo 10. Super Mario Galaxy Anti Piracy (v1.0 - E/P)
echo 11. Superstar Mario Galaxy (2023 - E/P)
echo.

set /P apply_mod=Select Mod number to apply to ISO/WBFS image: 

if %apply_mod%==1 (set mod=KMG1)

if %apply_mod%==2 (set mod=SMG1TC)

if %apply_mod%==3 (set mod=SMG1TGS)  

if %apply_mod%==4 (set mod=SMG1TKGS) 

if %apply_mod%==5 (set mod=SMG1UE)

if %apply_mod%==6 (set mod=SMG1NGE)  

if %apply_mod%==7 (set mod=SMG1CC)

if %apply_mod%==8 (set mod=SMG1DC)

if %apply_mod%==9 (set mod=SMG1TGSF)

if %apply_mod%==10 (set mod=SMGAP)

if %apply_mod%==11 (set mod=SSMG)

::=====================
:: Common build process
::=====================

CLS
echo.
echo ----- !%mod%[0]! Mod ISO Builder
echo ----- !%mod%[1]! !%mod%[2]!
echo.

set /P question1=Download Hack files (approx. size !%mod%[4]!)? (y/n): 
echo.

if %question1%==y GOTO :down_hack

if %question1%==n GOTO :question2

:question2

set /P question2=Already downloaded compressed hack file in the "mods" folder? (y/n): 
echo.

if %question2%==y GOTO :confirm_file

if %question2%==n GOTO :exit_1

:exit_1

echo Download Rom Hack files and try again.
echo Terminating program...
echo Press any key to close program.
echo.

pause >nul

exit

:down_hack

echo Downloading Rom Hack files...
echo.

%WGET% -q --show-progress -t inf "https://archive.org/download/SMG_1-2_Rom_Hacks/!%mod%[3]!" -P mods >nul

echo.

:confirm_file

if exist ".\mods\!%mod%[3]!" GOTO :extract_file

echo Rom Hack file not found.
echo Press any key to close program.
echo.

pause > nul

exit

:extract_file

echo Rom Hack file found.

echo Extracting file contents...

:: =========================================================
:: Win11 issue with double backslash extract path workaround
:: =========================================================

set "extractpath=.\mods\!%mod%[5]!"
set "fixextractpath=%extractpath:\\=\%"

:: =========================================================

%EXTRACT% x ".\mods\!%mod%[3]!" -o"%fixextractpath%" >nul

echo.

:build_iso

echo Building modded disc image...
echo This will take a while.
echo.

echo Extracting image contents...

%WIT% EXTRACT "%~f1" ".\temp" >nul

echo Copying and replacing mod files...

if exist ".\temp\DATA\" (
   xcopy ".\mods\!%mod%[6]!\!%mod%[7]!\*" ".\temp\DATA\files\" /E /Y >nul
) else (
   xcopy ".\mods\!%mod%[6]!\!%mod%[7]!\*" ".\temp\files\" /E /Y >nul)

:: ==================================
:: Redirector to mod's unique options
:: ==================================

if %mod%==KMG1 GOTO :KMG1_custom

if %mod%==SMG1TC GOTO :SMG1TC_custom

if %mod%==SMG1TKGS GOTO :SMG1TKGS_custom

if %mod%==SMG1CC GOTO :SMG1CC_custom

if %mod%==SMG1DC GOTO :SMG1DC_custom

if %mod%==SMGAP GOTO :SMGAP_custom

if %mod%==SSMG GOTO :SSMG_custom

:: ================================== 

:final_steps

echo Assembling game image...

%WIT% COPY ".\temp" ".\!%mod%[0]!.wbfs" >nul

echo Removing temp folders...

rmdir /s /q ".\temp"

rmdir /s /q ".\mods\!%mod%[6]!"

echo Editing game IDs...

%WIT% EDIT --id "!%mod%[8]!" ".\!%mod%[0]!.wbfs" >nul

%WIT% EDIT --tt-id "!%mod%[9]!" ".\!%mod%[0]!.wbfs" >nul

%WIT% EDIT --name "!%mod%[1]!" ".\!%mod%[0]!.wbfs" >nul

if exist ".\!%mod%[0]!.wbfs" GOTO :print-info
if not exist ".\!%mod%[0]!.wbfs" GOTO :print-error

:print-error

echo.
echo Error while modding the image.
echo Please try again.
echo Press any key to continue.
echo.

pause >nul

exit

:print-info

echo.
echo Disc image modded correctly.
echo.
echo Game ID: !%mod%[8]!
echo TMD ID: !%mod%[9]!
echo Game name: !%mod%[1]!
echo.
echo If you want to contribute (New Hack/Improve Tool) 
echo go to my GitHub repository and open an Issue.
echo Have fun^!
echo.
echo Press any key to exit.
echo.

pause >nul

exit


:: ===============================
:: Unique options/commands per mod
:: ===============================

:: ====
:: KMG1
:: ====

:KMG1_custom

echo.

xcopy ".\mods\!%mod%[6]!\US (NTSC)\UsEnglish" ".\temp\files\UsEnglish" /E /Y /I >nul

xcopy ".\mods\!%mod%[6]!\EU (PAL)\EuEnglish" ".\temp\files\EuEnglish" /E /Y /I >nul

echo Detected Region: %reg%

echo Applying hex patches... 

if %reg%==e (set KMG1[8]=KG1E01)

if %reg%==p (set KMG1[8]=KG1P01)

if exist ".\temp\DATA" (%PATCH% ".\temp\DATA\sys\main.dol" < ".\patches\!%mod%[8]!.patch" >nul)

if not exist ".\temp\DATA" (%PATCH% ".\temp\sys\main.dol" < ".\patches\!%mod%[8]!.patch" >nul)

echo.

GOTO :final_steps

:: ======
:: SMG1TC
:: ======

:SMG1TC_custom

echo.

echo Detected Region: %reg%

echo Applying hex patches... 

if %reg%==e (set KMG1[8]=G1TCE1)

if %reg%==p (set KMG1[8]=G1TCP1)

if %reg%==j (set KMG1[8]=G1TCJ1)

echo.

GOTO :final_steps

:: ========
:: SMG1TKGS
:: ========

:SMG1TKGS_custom

echo.

rmdir /s /q ".\mods\SMGTKGS 1.6.1"

echo Detected Region: %reg%

echo Applying hex patches... 

if %reg%==e (set SMG1TKGS[8]=1KGSE1)

if %reg%==p (set SMG1TKGS[8]=1KGSP1)

if %reg%==j (set SMG1TKGS[8]=1KGSJ1)

if exist ".\temp\DATA" (%PATCH% ".\temp\DATA\sys\main.dol" < ".\patches\!%mod%[8]!.patch" >nul)

if not exist ".\temp\DATA" (%PATCH% ".\temp\sys\main.dol" < ".\patches\!%mod%[8]!.patch" >nul)

echo.

GOTO :final_steps

:: ======
:: SMG1CC
:: ======

:SMG1CC_custom

echo.

echo Detected Region: %reg% 

echo Applying hex patches... 

if %reg%==e (set SMG1CC[8]=1CCE01)

if %reg%==p (set SMG1CC[8]=1CCP01)

if %reg%==j (set SMG1CC[8]=1CCJ01)

if exist ".\temp\DATA" (%PATCH% ".\temp\DATA\sys\main.dol" < ".\patches\!%mod%[8]!.patch" >nul)

if not exist ".\temp\DATA" (%PATCH% ".\temp\sys\main.dol" < ".\patches\!%mod%[8]!.patch" >nul)

echo.

GOTO :final_steps

:: ======
:: SMG1DC
:: ======

:SMG1DC_custom

echo.

echo Detected Region: %reg%

echo Applying hex patches... 

if %reg%==e (set SMG1DC[8]=1DCE01)

if %reg%==p (set SMG1DC[8]=1DCP01)

if exist ".\temp\DATA" (%PATCH% ".\temp\DATA\sys\main.dol" < ".\patches\!%mod%[8]!.patch" >nul)

if not exist ".\temp\DATA" (%PATCH% ".\temp\sys\main.dol" < ".\patches\!%mod%[8]!.patch" >nul)

echo.

GOTO :final_steps

:: =====
:: SMGAP
:: =====

:SMGAP_custom

echo.

echo Detected Region: %reg%

echo Applying hex patches... 

if %reg%==e (set SMGAP[8]=RMEAP1)

if %reg%==p (set SMGAP[8]=RMPAP1)

if exist ".\temp\DATA" (%PATCH% ".\temp\DATA\sys\main.dol" < ".\patches\!%mod%[8]!.patch" >nul)

if not exist ".\temp\DATA" (%PATCH% ".\temp\sys\main.dol" < ".\patches\!%mod%[8]!.patch" >nul)

echo.

GOTO :final_steps

:: ====
:: SSMG
:: ====

:SSMG_custom

echo.

xcopy ".\mods\!%mod%[6]!\US (NTSC)\UsEnglish" ".\temp\files\UsEnglish" /E /Y /I >nul

xcopy ".\mods\!%mod%[6]!\EU (PAL)\EuEnglish" ".\temp\files\EuEnglish" /E /Y /I >nul

echo Detected Region: %reg%

echo Applying hex patches... 

if %reg%==e (set SSMG[8]=SSMGE1)

if %reg%==p (set SSMG[8]=SSMGP1)

if exist ".\temp\DATA" (%PATCH% ".\temp\DATA\sys\main.dol" < ".\patches\!%mod%[8]!.patch" >nul)

if not exist ".\temp\DATA" (%PATCH% ".\temp\sys\main.dol" < ".\patches\!%mod%[8]!.patch" >nul)

echo.

GOTO :final_steps
